package com.wxb.controller;

import com.alibaba.fastjson.JSONObject;
import com.wxb.common.core.web.domain.AjaxResult;
import com.wxb.common.security.utils.SecurityUtils;
import com.wxb.domain.MallCollectEntity;
import com.wxb.service.IMallCollectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2025-05-30 16:31
 * @Version 1.0
 * @Description
 * @ClassName MallCollectController
 **/
@RestController
@RequestMapping("/collect")
public class MallCollectController {
    @Autowired
    private IMallCollectService collectService;
    /**
     * 得到request对象
     */
    @Autowired
    protected HttpServletRequest request;

    public JSONObject getJsonRequest() {
        JSONObject result = null;
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = request.getReader();) {
            char[] buff = new char[1024];
            int len;
            while ((len = reader.read(buff)) != -1) {
                sb.append(buff, 0, len);
            }
            result = JSONObject.parseObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
    /**
     * 获取用户收藏
     */
    @PostMapping("list")
    public Object list(Integer typeId) {

        Map<String, Object> param = new HashMap<>();
        param.put("userId", SecurityUtils.getUserId());
        param.put("typeId", typeId);
        List<MallCollectEntity> collectEntities = collectService.queryList(param);

        return AjaxResult.success(collectEntities);
    }

    /**
     * 获取用户收藏
     */
    @PostMapping("addordelete")
    public Object addordelete() {
        JSONObject jsonParam = getJsonRequest();
        Integer typeId = jsonParam.getInteger("typeId");
        Integer valueId = jsonParam.getInteger("valueId");

        Map<String, Object> param = new HashMap<>();
        param.put("userId", SecurityUtils.getUserId());
        param.put("typeId", typeId);
        param.put("valueId", valueId);
        List<MallCollectEntity> collectEntities = collectService.queryList(param);
        //
        Integer collectRes = null;
        String handleType = "add";
        if (null == collectEntities || collectEntities.size() < 1) {
            MallCollectEntity collectEntity = new MallCollectEntity();
            collectEntity.setAddTime(System.currentTimeMillis() / 1000);
            collectEntity.setTypeId(typeId);
            collectEntity.setValueId(valueId);
            collectEntity.setIsAttention(0);
            collectEntity.setUserId(SecurityUtils.getUserId());
            //添加收藏
            collectRes = collectService.save(collectEntity);
        } else {
            //取消收藏
            collectRes = collectService.delete(collectEntities.get(0).getId());
            handleType = "delete";
        }

        if (collectRes > 0) {
            Map<String, Object> data = new HashMap<>();
            data.put("type", handleType);
            return AjaxResult.success(data);
        }
        return AjaxResult.error("操作失败");
    }
}
